package com.easy.evaluation.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easy.common.bean.Evaluation;
import com.easy.common.service.IEvaluationService;
import com.easy.common.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin//允许跨域的
@RequestMapping("/evaluation")
public class EvaluationController {
    @Autowired
    IEvaluationService evaluationService;
    @GetMapping("/{id}")
    public Result getEvaluationById(@PathVariable Integer id) {
        return Result.success(evaluationService.getEvaluationById(id));
    }
    @PostMapping("/list")
    public Result getAll(@RequestBody Evaluation evaluation) {
        return Result.success(evaluationService.getAll(evaluation));
    }
    @PostMapping
    public Result addEvaluation(@RequestBody Evaluation evaluation) {
        int result = evaluationService.addEvaluation(evaluation);
        return result > 0 ? Result.success("添加成功", null) : Result.fail(500, "添加失败");
    }

    @PutMapping
    public Result updateEvaluation(@RequestBody Evaluation evaluation) {
        int result = evaluationService.updateEvaluation(evaluation);
        return result > 0 ? Result.success("更新成功", null) : Result.fail(500, "更新失败");
    }

    @DeleteMapping("/{id}")
    public Result deleteEvaluation(@PathVariable Integer id) {
        int result = evaluationService.deleteEvaluation(id);
        return result > 0 ? Result.success("删除成功", null) : Result.fail(500, "删除失败");
    }
    @PostMapping("page")
    public Result getPage(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, @RequestBody(required = false) Evaluation evaluation) {
        IPage<Evaluation> page = new Page<>(pageNum, pageSize);
        if(evaluation== null){
            evaluation = new Evaluation();
        }
        return Result.success(evaluationService.getPage(evaluation, new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(pageNum, pageSize)));
    }


}
